package com.zhai.boot.common;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @package: com.zhai.boot.common
 * @project-name: zhai-boot
 * @description: 分页
 * @author: Created by 翟凤玺
 * @create-datetime: 2018-8-7 11:40
 */
@Data
@AllArgsConstructor
public class Page<T> implements Serializable {
    /**
     * 总页数
     */
    private Integer totalPages;
    /**
     * 总行数
     */
    private Long totalRows;
    /**
     * 页码
     */
    private Integer pageNum;
    /**
     * 每页行数
     */
    private Integer pageSize;
    /**
     * 分页数据
     */
    private List<T> data;

    /**
     * 分页结果
     */
    public static <T> Page<T> result(Integer totalPages, Long totalRows, Integer pageNum, Integer pageSize, List<T> data) {
        return new Page<T>(totalPages, totalRows, pageNum, pageSize, data);
    }

    public static <T> Page<T> result(Long totalRows, Integer pageNum, Integer pageSize, List<T> data) {
        Integer totalPages = (int) (totalRows + pageSize - 1) / pageSize;
        return new Page<T>(totalPages, totalRows, pageNum, pageSize, data);
    }

    public static <T> Page<T> result(com.github.pagehelper.Page page) {
        return new Page<T>(page.getPages(), page.getTotal(), page.getPageNum(), page.getPageSize(), page.getResult());
    }
}
